<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/Form/FormBase.php';

class ListBox extends FormBase
{
	private $items = array();
	private $multiple = true;
	private $numLines = 6;
	private $enabled = true;

	function __construct()
	{
		parent::__construct();
	}

	function addItem($id, $name, $style = null)
	{
		$this->items[] = new ListBoxItem($id, $name, $style);
	}

	function setEnabled($enabled)
	{
		$this->enabled = $enabled;
	}

	/**
	 * Devuelve un ListBoxItem en dicha posicion
	 *
	 * @param Int $pos
	 * @return ListBoxItem
	 */
	function getItem($pos)
	{
		return $this->items[$pos];
	}

	function getItemCount()
	{
		return sizeof($this->items);
	}

	function setSelectedValues($array)
	{
		for ($j = 0; $j < sizeof($array); $j++) {
			for ($i = 0; $i < $this->getItemCount(); $i++) {
				$item = $this->getItem($i);
				if ($array[$j] == $item->getId()) {
					$item->setSelected(true);
					break;
				}
			}
		}
	}

	function setSelectedValue($item)
	{
		$this->setSelectedValues(array($item));
	}

	function selectCenteredValue()
	{
		if ($this->getItemCount() > 0)
			$this->setSelectedValue($this->getItem(round(($this->getItemCount() - 1) / 2))->getId());
	}

	function setMultiple($enabled)
	{
		$this->multiple = $enabled;
	}

	function setNumLines($lines)
	{
		$this->numLines = $lines;
	}

	function toString()
	{
		if ($this->getItemCount() <= 1) {
			$html = "<input type=\"hidden\"";
			if ($this->multiple)
				$html .= " name=\"" . $this->name . "[]\"";
			else
				$html .= " name=\"" . $this->name . "\"";

			$html .= " id=\"" . $this->id . "\"";

			$name = "";
			for ($i = 0; $i < $this->getItemCount(); $i++) {
				$item = $this->getItem($i);
				$html .= " value=\"" . $item->getId() . "\"";
				$name = $item->getName();
			}

			$html .= "><b>" . $name . "</b>";

			return $html;
		}

		$html = "<select";
		if (!$this->enabled)
			$html .= " disabled";

		if ($this->multiple)
			$html .= " name=\"" . $this->name . "[]\"";
		else
			$html .= " name=\"" . $this->name . "\"";

		$html .= " id=\"" . $this->name . "\"";
		if ($this->multiple)
			$html .= " multiple=\"multiple\"";

		if ($this->numLines > 1)
			$html .= " size=\"" . $this->numLines . "\"";

		$html .= ">";
		for ($i = 0; $i < $this->getItemCount(); $i++) {
			$item = $this->getItem($i);
			$html .= "<option value=\"" . $item->getId() . "\"";
			if ($item->getStyle() != null)
				$html .= " style=\"" . $item->getStyle() . "\"";

			if ($item->isSelected())
				$html .= " selected=\"selected\"";

			$html .= ">" . $item->getName() . "</option>";
		}

		$html .= "</select>";
		return $html;
	}
}

class ListBoxItem
{
	private $id;
	private $name;
	private $style = null;
	private $selected = false;

	function __construct($id, $name, $style)
	{
		$this->setId($id);
		$this->setName($name);
		$this->setStyle($style);
	}

	function getId()
	{
		return $this->id;
	}

	function getName()
	{
		return $this->name;
	}

	function getStyle()
	{
		return $this->style;
	}

	function isSelected()
	{
		return $this->selected;
	}

	function setId($id)
	{
		$this->id = $id;
	}

	function setName($name)
	{
		$this->name = $name;
	}

	function setStyle($style)
	{
		$this->style = $style;
	}

	function setSelected($selected)
	{
		$this->selected = $selected;
	}
}
?>